package com.gugee.ins.data.task.mark.service;

import com.gugee.ins.common.dto.RankTaskParam;
import com.gugee.ins.common.dal.service.mark.InsRkBloggerFlMonthDao;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.List;


/**
 * ins大博主国家、分类、粉丝数月排行表
 * @author huangxin
 */
@Component
@Slf4j
public class InsRkBloggerFlMonthSvc extends AbstractMarkRankProducerHandler {

    /**
     *
     */
    @Autowired
    InsRkBloggerFlMonthDao insRkBloggerFlMonthDao;

    /**
     * 处理ins大博主国家、分类、粉丝数月排行
     */
    @Override
    public void handleRank(RankTaskParam taskParam) {
        // 查找一级分类
        List<String> categoryList = getCategory(taskParam.getCategory(),true);
        // 查找头部国家
        List<String> nationList = getTopNationCode(taskParam.getNation(),true);

        Calendar calendar = Calendar.getInstance();

        // 设置时间
        if(!StringUtils.isEmpty(taskParam.getDateCode())){
            calendar.setTimeInMillis(getDateTime(taskParam.getDateCode()));
        }

        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        int lastDayOfRankMonth = Integer.parseInt(DateTimeUtils.formatSecond(calendar.getTimeInMillis() / 1000,
                "yyyyMMdd", taskConfig.getInsBloggerMarkDaytimeZone()));
        int rankMonth = lastDayOfRankMonth / 100;
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        int lastRankMonth = Integer.parseInt(DateTimeUtils.formatSecond(calendar.getTimeInMillis() / 1000,
                "yyyyMM", taskConfig.getInsBloggerMarkDaytimeZone()));

        // 各个月榜
        if(!categoryList.isEmpty()){
            if(!nationList.isEmpty()){
                for(String category : categoryList){
                    for(String nation : nationList){
                        if(category != null && nation != null){
                            markRank(lastDayOfRankMonth,rankMonth,lastRankMonth,nation,category);
                        }
                    }
                }
            }else{
                // nationList 为空时
                for(String category : categoryList){
                    if(category != null){
                        markRank(lastDayOfRankMonth,rankMonth,lastRankMonth,"",category);
                    }
                }
            }
        }else if(!nationList.isEmpty()){
            // categoryList 为空时
            for(String nation : nationList){
                if(nation != null){
                    markRank(lastDayOfRankMonth,rankMonth,lastRankMonth,nation,"");
                }
            }
        }

        // 更新榜单上的平均点赞数和平均er值
        insRkBloggerFlMonthDao.updateErAndLike(rankMonth,System.currentTimeMillis() / 1000 - 30 * 24 *3600);
    }

    /**
     *
     * @param lastDayOfRankMonth
     * @param rankMonth
     * @param lastRankMonth
     * @param nation
     * @param category
     */
    public void markRank(int lastDayOfRankMonth,int rankMonth,int lastRankMonth,String nation,String category){
        try{
            insRkBloggerFlMonthDao.insertRkBloggerFlMonth(lastDayOfRankMonth,rankMonth,lastRankMonth,nation,category);
        }catch (Exception e){
            e.printStackTrace();
            log.info("ins大博主国家、分类、粉丝数月榜单 生成失败 rankMonth:{},nation:{},category:{},msg:{}", rankMonth,nation,category,e.getMessage());
            XxlJobLogger.log("ins大博主国家、分类、粉丝数月榜单 生成失败 rankMonth:{},nation:{},category:{},msg:{}",rankMonth,nation,category,e.getMessage());
        }
    }
}
